package com.wjk.cms.util.wx;

import com.alibaba.fastjson.JSONObject;
import com.wjk.cms.common.Result;
import com.wjk.cms.util.CommonUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

@Data
public class WxConfig {

    private static WxConfig instance = new WxConfig();

   /* // 微信支付的商户id
    private String mchId =  "1231245902";//"1600611292";

    // 微信支付的商户密钥
    private String mchKey = "XieLi13505313194YouYou1856075075";//"YuanYangGuoLvQingDaoFenGongSi123";

    // 小程序appid
    private String appId = "wxb7295585a279ae60";// "wx3c8f513e536b7799";

    // 小程序secret
    private String appSecret = "2292b072ffc67ed6e06fa93b3ee88480";//"a391a31ba07b278388522288d48819c1";
*/
    // 微信支付的商户id
    private String mchId =  "1615696653";

    // 微信支付的商户密钥
    private String mchKey = "shandonggfrh123654789xinyaogfrh1";

    // 小程序appid
    private String appId = "wx8e57f2eba5f2ca51";

    // 小程序secret
    private String appSecret = "06557f7ef485c192d529f0c4f50a536d";

    // 微信统一下单接口地址
    private String unifiedOrderUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    // 企业付款到零钱
    private String payUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

    // 签名方式
    private String signType = "MD5";

    // 交易类型
    private String tradeTypeApp = "APP";

    private String tradeTypeXcx = "JSAPI";

    private String tradeTypeH5 = "MWEB";



    // 公众号appid
    private String gzhAppId = "wx058959b417709a44";

    // 公众号secret
    private String gzhAppSecret = "2cd21b61c39c8c1226c68545ee0204c6";


    private WxConfig() {

    }

    // 微信支付的商户id
    public static String MCH_ID() {
        return instance.getMchId();
    }

    // 微信支付的商户密钥
    public static String MCH_KEY() {
        return instance.getMchKey();
    }

    // 微信统一下单接口地址
    public static String UNIFIED_ORDER_URL() {
        return instance.getUnifiedOrderUrl();
    }

    // 企业付款到零钱
    public static String PAY_URL() {
        return instance.getPayUrl();
    }

    // 签名方式
    public static String SIGN_TYPE() {
        return instance.getSignType();
    }

    // 交易类型
    public static String TRADE_TYPE_APP() {
        return instance.getTradeTypeApp();
    }

    public static String TRADE_TYPE_XCX() {
        return instance.getTradeTypeXcx();
    }

    public static String TRADE_TYPE_H5() {
        return instance.getTradeTypeH5();
    }

    // 公众号appid
    public static String GZH_APP_ID() {
        return instance.getGzhAppId();
    }

    // 公众号secret
    public static String GZH_APP_SECRET() {
        return instance.getGzhAppSecret();
    }

    // 小程序appid
    public static String APP_ID() {
        return instance.getAppId();
    }

    // 小程序secret
    public static String APP_SECRET() {
        return instance.getAppSecret();
    }

    public static void main(String[] args) {

        String openid = "oTLPr4hEaEUNHntYVcd_bJ8Y93Tw"; // 业务数据
        String appid = WxConfig.APP_ID();
        String mch_id =WxConfig.MCH_ID(); // 租户配置
        String nonce_str = CommonUtil.generateNonceStr();
        String device_info = "wjk"; // 租户配置
        String body = "王浆卡"; // 业务参数
        String attach = "123123"; // 业务数据
        String out_trade_no = "123412";
        String moneyInFen = CommonUtil.getMoneyInFen("0.01");
        String spbill_create_ip = "";
        String notify_url =  "/auth/pay/notify/wx";
        String trade_type = WxConfig.TRADE_TYPE_XCX();

        SortedMap<String, String> packageParams = new TreeMap<>();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
//        packageParams.put("sub_mch_id", "1535043681");
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("device_info", device_info);
        packageParams.put("body", body);
        packageParams.put("attach", attach);
        packageParams.put("out_trade_no", out_trade_no);
        packageParams.put("total_fee", moneyInFen);
        packageParams.put("spbill_create_ip", spbill_create_ip);
        packageParams.put("notify_url", notify_url);
        packageParams.put("trade_type", trade_type);
        packageParams.put("openid", openid);
        String sign = WxPayUtil.createPackageSign(packageParams);

        String xml = "<xml>"
                + "<appid>" + appid + "</appid>"
                + "<mch_id>" + mch_id + "</mch_id>"
//                + "<sub_mch_id>1535043681</sub_mch_id>"
                + "<device_info>" + device_info + "</device_info>"
                + "<nonce_str>" + nonce_str + "</nonce_str>"
                + "<sign>" + sign + "</sign>"
                + "<body><![CDATA[" + body + "]]></body>"
                + "<attach><![CDATA[" + attach + "]]></attach>"
                + "<out_trade_no>" + out_trade_no + "</out_trade_no>"
                + "<total_fee>" + moneyInFen  + "</total_fee>"
                + "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>"
                + "<notify_url>" + notify_url + "</notify_url>"
                + "<trade_type>" + trade_type + "</trade_type>"
                + "<openid>" + openid + "</openid>"
                + "</xml>";
        try {
            String prepayId = WxPayUtil.unifiedOrder(xml).get("prepay_id");
            if (StringUtils.isEmpty(prepayId)) {
                System.out.println("微信统一下单接口出错");
            } else {
                String timestamp = CommonUtil.getTimeStamp();
                String packageStr = "prepay_id=" + prepayId;

                List<NameValuePair> signParams = new LinkedList<>();
                signParams.add(new BasicNameValuePair("appId", appid));
                signParams.add(new BasicNameValuePair("nonceStr", nonce_str));
                signParams.add(new BasicNameValuePair("package", packageStr));
                signParams.add(new BasicNameValuePair("signType", "MD5"));
                signParams.add(new BasicNameValuePair("timeStamp", timestamp));
                String paySign = WxPayUtil.createPaySign(signParams);

                // 带到预支付页面的参数值
                JSONObject data = new JSONObject();
                data.put("nonceStr", nonce_str);
                data.put("timeStamp", timestamp);
                data.put("package", packageStr);
                data.put("paySign", paySign);
                data.put("signType", WxConfig.SIGN_TYPE());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
